class EditorNewsController < ApplicationController
  before_filter :authenticate
  layout "editor"
  
  # GET /news
  # GET /news.xml
  def index
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('EDITOR'))
    
    @filter = params[:filter].nil? ? {} : params[:filter]
    @news = News.editor_news_list(current_user, @filter).paginate :page=>params[:page], :order=>'created_at desc', :per_page => 5
    
    # activities table
    @articles_approving = Article.articles_approving
    @articles_new = Article.articles_new
    
    # events
    @events = Event.all
    
    #user
    @users = User.all
    
    # event_statuese
    @event_statuses = EventStatus.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @news }
    end
  end

  # GET /news/1
  # GET /news/1.xml
  def show
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('EDITOR'))
    
    @news = News.find(params[:id])
    
    # activities table
    @articles_approving = Article.articles_approving
    @articles_new = Article.articles_new

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @news }
    end
  end

  # GET /news/new
  # GET /news/new.xml
  def new
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('EDITOR'))
    
    @news = News.new
    @events = Event.all.collect {|p| [ p.title, p.id ] }
    @images = Image.all
    
    # activities table
    @articles_approving = Article.articles_approving
    @articles_new = Article.articles_new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @news }
    end
  end

  # GET /news/1/edit
  def edit
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('EDITOR'))
    
    @news = News.find(params[:id])
    @events = Event.all.collect {|p| [ p.title, p.id ] }
    @images = Image.all
    
    # activities table
    @articles_approving = Article.articles_approving
    @articles_new = Article.articles_new
    
  end

  # POST /news
  # POST /news.xml
  def create
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('EDITOR'))
    
    @news = News.new(params[:news])
    @news.user = current_user
    
    @events = Event.all.collect {|p| [ p.title, p.id ] }
    @images = Image.all
    
    # activities table
    @articles_approving = Article.articles_approving
    @articles_new = Article.articles_new

    respond_to do |format|
      if @news.save
        format.html { redirect_to(url_for({:action => "index"}), :notice => 'News was successfully created.') }
        format.xml  { render :xml => @news, :status => :created, :location => @news }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @news.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /news/1
  # PUT /news/1.xml
  def update
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('EDITOR'))
    
    @news = News.find(params[:id])

    respond_to do |format|
      if @news.update_attributes(params[:news])
        format.html { redirect_to(url_for({:action => "index"}), :notice => 'News was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @news.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def save
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('EDITOR'))
    
    if !params[:id].nil?
      @news = News.find(params[:id])
      
      # CHANGE STATUS
      @news.news_status = NewsStatus.get('MODIFY')

      respond_to do |format|
        if @news.update_attributes(params[:news])
          format.html { redirect_to(url_for({:action => "index"}), :notice => 'News was successfully updated.') }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @news.errors, :status => :unprocessable_entity }
        end
      end
    else
      @news = News.new(params[:news])
      @news.user = current_user
      
      # CHANGE STATUS
      @news.news_status = NewsStatus.get('NEW')
      
      @events = Event.all.collect {|p| [ p.title, p.id ] }
      @images = Image.all
      
      # activities table
      @articles_approving = Article.articles_approving
      @articles_new = Article.articles_new
  
      respond_to do |format|
        if @news.save
          format.html { redirect_to(url_for({:action => "index"}), :notice => 'News was successfully created.') }
          format.xml  { render :xml => @news, :status => :created, :location => @news }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @news.errors, :status => :unprocessable_entity }
        end
      end
    end    
  end

  # DELETE /news/1
  # DELETE /news/1.xml
  def destroy
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('EDITOR'))
    
    @news = News.find(params[:id])
    @news.destroy

    respond_to do |format|
      format.html { redirect_to(url_for({:action => "index"})) }
      format.xml  { head :ok }
    end
  end
  
  def insert_image_box    
    @images = Event.find(params[:id]).images
    render :layout => false
  end
  
  def delete_selected
    raise Exceptions::UserAccessError if !current_user.isRole?(Role.find_by_alias('EDITOR'))
    
    @ids = params[:ids]
    @user = current_user
    
      @ids.each do |obj|
      @news = News.find(obj[0])
          @news.destroy
      end
    
    
    respond_to do |format|
        format.html { redirect_to({:controller => 'editor_news'}, :notice => "Articles was successfully deleted.") }
        format.xml  { head :ok }
        format.js { render 'destroy.js.erb' }
    end
  end
  
end
